package com.wyd.utils;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * XIAOJING
 */
public class IDUtils {

    private static SnowflakeIdWorker idWorker = new SnowflakeIdWorker();
    private static final int HEIGHT_LINE = 9999;
    private static final int ZERO_LINE = 1000;
    private static AtomicInteger seq = new AtomicInteger(ZERO_LINE);

    /**
     * @return
     */
    public static String getID() {
        String currTimeFormat = DateUtils.format(DateUtils.nowLdt(), DateUtils.YYYYMMDDHHMMSSSSS_STR);
        return currTimeFormat + getRandomNumStr(5) + getIncrSeq();
    }

    /**
     * @return
     */
    public static String getID(LocalDateTime localDateTime) {
        String currTimeFormat = DateUtils.format(localDateTime, DateUtils.YYYYMMDDHHMMSSSSS_STR);
        return currTimeFormat + getRandomNumStr(2);
    }


    /**
     * 生成有序自增ID， 去分布式
     *
     * @return
     */
    public static long getUniqueId() {
        return idWorker.nextId();
    }

    public static String getUniqueId(String pre) {
        StringBuilder builder = new StringBuilder();
        if (StringUtils.isNotEmpty(pre)) {
            builder.append(pre);
        }
        builder.append(getUniqueId());
        return builder.toString();
    }

    public static String getID(String preStr) {
        String id = getID();
        if (!StringUtils.isEmpty(preStr)) {
            id = preStr + id;
        }
        return id;
    }

    private static String getIncrSeq() {
        int i = seq.addAndGet(1);
        if (i > HEIGHT_LINE) {
            synchronized (seq) {
                if (seq.get() > HEIGHT_LINE) {
                    seq.set(ZERO_LINE);
                }
            }
            return getIncrSeq();
        }
        return String.valueOf(i);
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().trim().replace("-", "");
    }

    /**
     * 获取固定长度的随机数,注意前缀可能为0开头
     *
     * @param strLength
     * @return
     */
    public static String getRandomNumStr(int strLength) {
        Random rm = new Random();
        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);
        // 将获得的获得随机数转化为字符串
        String fixLengthString = String.valueOf(pross);
        // 返回固定的长度的随机数
        return fixLengthString.substring(1, strLength + 1);
    }

    private static AtomicInteger CM_IOT_SEQ = new AtomicInteger(0);

    private static int MAX_ID = 99;

    private static int getSeq() {
        int i = CM_IOT_SEQ.addAndGet(1);
        if (i > MAX_ID) {
            synchronized (CM_IOT_SEQ) {
                if (CM_IOT_SEQ.get() > MAX_ID) {
                    CM_IOT_SEQ.set(0);
                }
            }
            return getSeq();
        }
        return i;
    }


    public static String getCmOnlineSeq(int len) {
        String s = String.valueOf(getSeq());
        if (len <= 1) return s;
        while (s.length() < len) {
            s = "0" + s;
        }
        return s;
    }


    public static void main(String[] args) throws InterruptedException {
        System.out.println(getUniqueId());
        System.out.println(getID());
        System.out.println(getSeq());
    }


}
